package com.ruoyi.biz.service.caseinfoitem.impl;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.biz.bean.bo.FileGroup;
import com.ruoyi.biz.domain.CaseInfoClosedItem;
import com.ruoyi.biz.domain.diskfile.FileInfo;
import com.ruoyi.biz.mapper.CaseInfoClosedItemMapper;
import com.ruoyi.biz.service.caseinfoitem.ICaseInfoClosedItemService;
import com.ruoyi.biz.service.diskfile.IFileItemStoreService;
import com.ruoyi.comm.crud.service.impl.BaseImpls;

/**
 * 案件类型信息 服务层处理
 * 
 * @author riverbo
 */
@Service
public class CaseInfoClosedItemServiceImpl extends BaseImpls<CaseInfoClosedItemMapper, CaseInfoClosedItem> implements ICaseInfoClosedItemService {

	@Autowired
	IFileItemStoreService storeService;

//	@Override
//	public Boolean checkNameUnique(CaseType t) {
//        Long boId = StringUtils.isNull(t.getCaseTypeId()) ? -1L : t.getCaseTypeId();
//        Long dbId = this.baseMapper.getIdByName(t);
//        if (StringUtils.isNotNull(dbId) && dbId.longValue() != boId.longValue()) {
//            return UserConstants.NOT_UNIQUE;
//        }
//        return UserConstants.UNIQUE;
//	}
	
	public Map<Integer, List<CaseInfoClosedItem>> getMap(Integer caseId) {
		Map<Integer, List<CaseInfoClosedItem>> map = null;
//		List<CaseInfoPhaseItem> list = this.selectListById(caseId);
//		if (!CollectionUtils.isEmpty(list)) {
//			map = new LinkedHashMap<>();
//			for (CaseInfoPhaseItem u : list) {
//				List<CaseCust> ls = map.get(u.getCaseCustType());
//				if (ls == null) {
//					ls = new ArrayList<>();
//				}
//				ls.add(u);
//				map.put(u.getCaseCustType(), ls);
//			}
//		}
		return map;
	}
	
	@Override
	public List<CaseInfoClosedItem> selectListAttachById(Number id) {
		List<CaseInfoClosedItem> ls = super.selectListById(id);
		readAttachById(ls);
		return ls;
	}
	
	@Override
	public void readAttachById(List<CaseInfoClosedItem> ls) {
		for (CaseInfoClosedItem u:ls) {
			Integer caseId = u.getCaseId();
			int idx = u.getCasePhaseIdx();
		    String sub = String.format("caseinfo/%d/%d/%d", caseId, u.getCasePhaseId(), idx);
		    Stream<Path> ll = storeService.loadAll(sub);
		    List<FileInfo> fileInfos = new ArrayList<>();
		    if (ll != null) {
			    fileInfos = ll.map(path -> {
			      String filename = path.getFileName().toString();
			      String url = "";//MvcUriComponentsBuilder
			          //.fromMethodName(FileItemController.class, "getFile", path.getFileName().toString()).build().toString();
			      return new FileInfo(filename, url);
			    }).collect(Collectors.toList());
		    }
		    //
		    FileGroup group = new FileGroup();
		    group.setCaseId(caseId);
		    group.setIdx(idx);
		    group.setFiles(fileInfos);
		    u.setGroup(group);
		}
	}

	@Override
	public int insert(Integer id, List<CaseInfoClosedItem> list) {
		if (!CollectionUtils.isEmpty(list)) {
			for (CaseInfoClosedItem u : list) {
				u.setCaseId(id);
			}
			return super.insert(list);
		} else {
			return 0;
		}
	}
	
}
